package com.zhs.gccs.view.taskMap;

import android.content.Context;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.PopupWindow;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.zhs.gccs.R;
import com.zhs.gccs.model.MapBasicLayer;
import com.zhs.gccs.adapter.LayerAdapter;
import com.zhs.gccs.view.BaseView;
import com.zhs.gccs.utils.PopupWindowUtils;
import com.zhs.gccs.utils.ScreenUtils;

import java.util.ArrayList;
import java.util.List;

public class MapToolView extends BaseView implements View.OnClickListener{
    private View ll_zoom_china;
    private View ll_poi_search;
    private View ll_small;
    private View ll_big;
    private View ll_location;
    private View ll_add_nav;
    private View ll_map;
    private PopupWindow layerPopwindow;
    private RecyclerView rv_layerViews;
    private LayerAdapter layerAdapter;
    private List<MapBasicLayer> basicLayers;
    private int offsetX,offsetY;

    public MapToolView(@NonNull Context context) {
        super(context);
    }

    public MapToolView(@NonNull Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
    }

    public MapToolView(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @Override
    public void init(Context context, AttributeSet attrs, int defStyleAttr) {
        View view = LayoutInflater.from(context).inflate(R.layout.layout_view_mapbox_tools, this, true);
//        TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.CustomerLogo);
//        String title = typedArray.getString(R.styleable.CustomerInfoItem_preText);
//        typedArray.recycle();
        ll_zoom_china = view.findViewById(R.id.ll_zoom_china);
        ll_poi_search = view.findViewById(R.id.ll_poi_search);
        ll_small = view.findViewById(R.id.ll_small);
        ll_big = view.findViewById(R.id.ll_big);
        ll_location = view.findViewById(R.id.ll_location);
        ll_add_nav = view.findViewById(R.id.ll_add_nav);
        ll_map = view.findViewById(R.id.ll_map);
        ll_poi_search.setOnClickListener(this);
        ll_zoom_china.setOnClickListener(this);
        ll_small.setOnClickListener(this);
        ll_big.setOnClickListener(this);
        ll_location.setOnClickListener(this);
        ll_add_nav.setOnClickListener(this);
        ll_map.setOnClickListener(this);
    }

    @Override
    protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
        super.onLayout(changed, left, top, right, bottom);
        float v = ll_map.getY() + ll_map.getHeight()/2+ScreenUtils.dpToPxInt(context,150)/2;
        offsetY = ScreenUtils.dpToPxInt(context,40);//(int) (ScreenUtils.getScreenHeightPx(context)-v);
        offsetX = ScreenUtils.dpToPxInt(context,65);//10 45 10
    }

    public MapToolView loadData(){
        rv_layerViews = (RecyclerView) LayoutInflater.from(context).inflate(R.layout.layout_view_layers, null);
        layerPopwindow = PopupWindowUtils.initPopupWindow(context, rv_layerViews);
        basicLayers = new ArrayList<>();
        basicLayers.add(new MapBasicLayer(R.mipmap.mapbox,"mapbox",MapBasicLayer.type_mapbox));
        basicLayers.add(new MapBasicLayer(R.mipmap.icon_map_baidu,"百度",MapBasicLayer.type_baidu));
//        float v = ll_map.getY() - ll_map.getHeight()+ScreenUtils.dpToPxInt(context,150)/2;
        layerAdapter = new LayerAdapter(context,basicLayers);
        rv_layerViews.setLayoutManager(new LinearLayoutManager(context, LinearLayoutManager.HORIZONTAL, false));
        rv_layerViews.setAdapter(layerAdapter);
        layerAdapter.setOperationListener(new LayerAdapter.OperationListener() {
            @Override
            public void onItemClick(int position) {
                layerPopwindow.dismiss();
                if(mapToolListener!=null){
                    mapToolListener.changeBasicLayer(basicLayers.get(position));
                }

            }
        });
        return this;
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.ll_poi_search:
                if(this.mapToolListener!=null){
                    mapToolListener.openSearchPOI();
                }
                break;
            case R.id.ll_zoom_china:
                zoomToChina();
                break;
            case R.id.ll_small:
                zoom(-1);
                break;
            case R.id.ll_big:
                zoom(1);
                break;
            case R.id.ll_location:
                location();
                break;
            case R.id.ll_add_nav:
                addNavMarker();
                break;
            case R.id.ll_map://https://blog.csdn.net/u012556114/article/details/123484733
                if (layerPopwindow.isShowing()) {
                    layerPopwindow.dismiss();
                } else {//https://blog.csdn.net/Justwen26/article/details/61621076
                    layerPopwindow.showAtLocation(ll_map,
                        Gravity.RIGHT|Gravity.BOTTOM ,offsetX,offsetY);
//                    layerPopwindow.showAsDropDown(ll_map,-ll_map.getWidth(),10,Gravity.LEFT);
                }
                break;
        }
    }

    public void setAddLocationMarkerVisable(boolean visable) {
        ll_add_nav.setVisibility(visable?VISIBLE:GONE);
    }

    private void addNavMarker() {
        if(this.mapToolListener!=null){
            mapToolListener.addMarkerOfNav();
        }
    }

    private void location() {
        if(this.mapToolListener!=null){
            mapToolListener.locationToUser();
        }
    }

    private void zoom(int zoom) {
        if(this.mapToolListener!=null){
            mapToolListener.zoom(zoom);
        }
    }

    private void zoomToChina() {
        //{center:[104.1954,35.8617],zoom:3}
        if(this.mapToolListener!=null){
            mapToolListener.zoomToChina();
        }
    }
    private MapToolListener mapToolListener;

    public MapToolListener getMapToolListener() {
        return mapToolListener;
    }

    public void setMapToolListener(MapToolListener mapToolListener) {
        this.mapToolListener = mapToolListener;
    }



    public interface MapToolListener{
        void zoomToChina();
        void zoom(int zoom);
        void locationToUser();

        void addMarkerOfNav();

        void changeBasicLayer(MapBasicLayer basicLayer);

        void openSearchPOI();
    }
}
